package com.teamide.util;

import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class ResponseUtil {

	public static void out(HttpServletResponse response, String content) {

		PrintWriter printWriter = null;
		response.setCharacterEncoding("UTF-8");
		try {
			printWriter = response.getWriter();
			printWriter.print(content);
			printWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(printWriter);
		}

	}

	public static void outJS(HttpServletResponse response, String content) {

		PrintWriter printWriter = null;
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/x-javascript");
		try {
			printWriter = response.getWriter();
			printWriter.print(content);
			printWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(printWriter);
		}

	}

	public static void outCSS(HttpServletResponse response, String content) {

		PrintWriter printWriter = null;
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/css");
		try {
			printWriter = response.getWriter();
			printWriter.print(content);
		} catch (Exception e) {
			e.printStackTrace();
			printWriter.flush();
		} finally {
			IOUtil.close(printWriter);
		}

	}

	public static void outHTML(HttpServletResponse response, String content) {

		response.setHeader("Content-type", "text/html;charset=UTF-8");
		PrintWriter printWriter = null;
		response.setCharacterEncoding("UTF-8");
		try {
			printWriter = response.getWriter();
			printWriter.print(content);
			printWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(printWriter);
		}
	}

	public static void outJSON(HttpServletResponse response, Object data) {

		response.setHeader("Content-type", "text/json;charset=UTF-8");
		PrintWriter printWriter = null;
		response.setCharacterEncoding("UTF-8");
		try {
			printWriter = response.getWriter();
			if (data != null) {
				if (data instanceof String) {
					printWriter.print(data);
				} else {
					printWriter.print(JSON.toJSONString(data, SerializerFeature.WriteMapNullValue));
				}
			}
			printWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(printWriter);
		}
	}

	public static void outText(HttpServletResponse response, String content) {

		response.setHeader("Content-type", "application/text;charset=UTF-8");
		PrintWriter printWriter = null;
		response.setCharacterEncoding("UTF-8");
		try {
			printWriter = response.getWriter();
			printWriter.print(content);
			printWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(printWriter);
		}
	}

	public static void outFile(HttpServletResponse response, String filename, byte[] bytes) {
		ServletOutputStream sout = null;
		try {
			response.addHeader("Content-Disposition", "attachment; filename=" + filename);
			response.setContentType("application/octet-stream");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Connection", "close");
			sout = response.getOutputStream();
			sout.write(bytes);
			sout.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(sout);
		}
	}

}
